package com.xx.wars.screen;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.xx.framework.Game;
import com.xx.framework.Input.TouchEvent;
import com.xx.framework.gl.Camera2D;
import com.xx.framework.gl.SpriteBatcher;
import com.xx.framework.gl.TextureRegion;
import com.xx.framework.impl.GLScreen;
import com.xx.framework.math.Vector2;
import com.xx.wars.util.Assets;
import com.xx.wars.util.GameSetting;

public class SplashScreen extends GLScreen {

	private Camera2D guiCam;
	private Vector2 touchPoint;
	private SpriteBatcher batcher;
	
	private float blueValue = 0.7f;
	private boolean addColor = false;
	
	public SplashScreen(Game game) {
		super(game);
		initData();
	}

	private void initData() {
		touchPoint = new Vector2();
		guiCam = new Camera2D(glGraphics, GameSetting.SCREEN_WIDTH,
				GameSetting.SCREEN_HEIGHT);
		batcher = new SpriteBatcher(glGraphics, 100);
	}

	@Override
	public void update(float deltaTime) {
		if(addColor){
			blueValue += 0.05;
		}else{
			blueValue -= 0.05;
		}
		if(blueValue >= 1.0){
			addColor = false;
		}
		if(blueValue <= 0.3f){
			addColor = true;
		}
		
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		int len = touchEvents.size();

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			touchPoint.set(event.x, event.y);
			guiCam.touchToWorld(touchPoint);
			if (event.type == TouchEvent.TOUCH_DOWN) {
			}
			if (event.type == TouchEvent.TOUCH_DRAGGED) {
			}
			if (event.type == TouchEvent.TOUCH_UP) {
				System.out.println("on all right , i'm agreed you change screen");
				game.setScreen(new GameMenu(game));
			}
		}
	}
	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		guiCam.setViewportAndMatrices();
		
		gl.glColor4f(1.0f, 1.0f, 1.0f, blueValue);
		TextureRegion tr = Assets.createTextureRegion("logo.png");
		batcher.beginBatch(tr.texture);
		batcher.drawAbsoSprite(0, 0, 
				GameSetting.SCREEN_WIDTH,GameSetting.SCREEN_HEIGHT, tr);
		batcher.endBatch();
		
		gl.glDisableClientState(GL10.GL_TEXTURE_2D);
		gl.glDisableClientState(GL10.GL_BLEND);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

}
